<template>
	<view class="">
		<view class="app-logo" @longpress="longPress">
			<image src="../../static/img/logo.png" mode=""></image>
		</view>
		<view class="">
			<view class="app-name">{{appname}}</view>
			<view class="app-version">Version {{version}}</view>
		</view>
		<view class="list">
			<view class="list-item"  hover-class="clickable" @click="update">
				<view class="">版本更新</view>
				<uni-icons type="right" size="20"></uni-icons>
			</view>
			<view class="list-item"  hover-class="clickable" @click="goHistoryVersion">
				<view class="">功能介绍</view>
				<uni-icons type="right" size="20"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import _custom from '../../common/_custom.js'
	import _data from '@/common/_data.js'
	import { debuggerModule, installDebugger } from 'uni_modules/imengyu-IMDebuggerWindow/common/debuggerExtern.js'//调试组件
	
	export default {
		data() {
			return {
				appname: _data.data('appname'),
				version: _data.data('version'),
				platform: '',
				showDebug: false,
			}
		},
		onLoad() {
			
		},
		methods: {
			update() {
				// #ifdef APP-PLUS
				let _this = this;
				this.platform = uni.getSystemInfoSync().platform;
				plus.runtime.getProperty(plus.runtime.appid, function(info) {
					_custom.getDataJava({
						apiname: 'getAppLatestVersion',
						apiparam: {
							appname: info.name,
							platform: _this.platform
						},
						success(res){
							if(res[0].AppVersion > info.version) {
								uni.showModal({
									title: '提示',
									content: '发现新版本，是否开始更新？',
									success(e) {
										if (e.confirm) {
											if(_this.platform == 'ios'){
												if(!res[0].IosPath) return
												_this.iosUrl = res[0].IosPath;
												uni.navigateTo({
													url: '/pages/push/web?url=' + _this.iosUrl,
												});
											} else {
												let wgtWaiting = plus.nativeUI.showWaiting("更新开始下载"),
												update_url = (plus.os.name == 'Android' ? res[0].ApkPath.replace('homepage',_data.data('static_img') + '/homepage') : '')
												let downloadTask = uni.downloadFile({
													url: update_url,
													success: (res) => {
														wgtWaiting.close();
														if (res.statusCode === 200 ) {
															plus.runtime.install(res.tempFilePath,{},() => {
																plus.runtime.restart();
															},(e) => {
																uni.showModal({
																	content: "update error [" + e.code + "]：" + e.message,
																	showCancel: false,
																});
															});
														} else {
															uni.showModal({
																content: "地址错误!",
																showCancel: false,
															});
														}
													}
												});
												downloadTask.onProgressUpdate((res) => {
													wgtWaiting.setTitle('下载中...' + res.progress + '%');
												});
											} 
										}
									}
								})
							} else {
								uni.showToast({
									title: '已经是最新版',
									icon: 'none'
								})
							}
						},
						fail() {
							uni.showToast({
								title: '无法获取版本信息',
								icon: 'none'
							})
						}
					});
				});
				// #endif
			},
			goHistoryVersion() {
				uni.navigateTo({
					url: './history_version'
				})
			},
			longPress() {
				if(this.showDebug == false) {
					this.showDebug = true;
					installDebugger({
						enableRequestInterceptor: false, //默认为false，指示是否拦截网络请求，参见下一条
						showGlobalFloatWindow: this.showDebug //默认为true，指定是否添加一个全局的调试按钮，点击可跳转至窗口
					})
				}
			}
		}
	}
</script>

<style>
	.app-logo {
		width: 100%;
		height: 120rpx;
		text-align: center;
		margin-top: 100rpx;
	}
	
	.app-logo > image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 20rpx;
	}
	
	.app-name {
		font-size: 40rpx;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
	}
	
	.app-version {
		font-size: 30rpx;
		text-align: center;
	}
	
	.list {
		margin-top: 60rpx;
	}
	
	.list > .list-item:last-child {
		border-bottom: 1rpx solid #eee;
	}
	
	.list-item {
		padding: 0 60rpx;
		height: 100rpx;
		font-size: 32rpx;
		background-color: rgba(255,255,255,.2);
		border-top: 1rpx solid #eee;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.clickable {
		background-color: rgba(0,0,0,.1);
	}
</style>